package com.world.cat.service.sys;

import com.world.cat.dao.sys.AreaDao;
import com.world.cat.dao.sys.UserAreaDao;
import com.world.cat.model.sys.Area;
import com.world.cat.model.sys.UserArea;
import com.world.cat.pojo.TreeNode;
import com.world.common.util.CollectionUtil;
import com.world.common.util.SysUtil;
import com.world.core.dao.GenericRepository;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AreaService extends BaseService<Area> {
    @Resource
    private AreaDao areadao;
    @Resource
    private UserAreaDao userAreaDao;
    @Override
    public GenericRepository getDao() {
        return areadao;
    }

    public TreeNode getAreaTree(String userId, String rootAreaId) {
        Map<String, Integer> allOnAreaMap = new HashMap<>();
        //获取用户的权限，展示区域树的时候将对应的区域勾选
        List<UserArea> userAreas = userAreaDao.findByUserId(userId);
        Map<String, String> checkMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(userAreas)) {
            for (UserArea userArea : userAreas) {
                checkMap.put(userArea.getAreaId(), userArea.getUserId());
            }
        }
        List<Area> areas = areadao.findAll();
        Map<String, List<Area>> areaMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(areas)) {
            for (Area a : areas) {
                List<Area> areas1 = areaMap.get(a.getParentId());
                if (areas1 == null) {
                    areas1 = new ArrayList<>();
                    areaMap.put(a.getParentId(), areas1);
                }
                areas1.add(a);
            }
        }
        TreeNode node = new TreeNode();
        node.setName("根");
        node.setId(rootAreaId);
        if (checkMap != null && SysUtil.isNotEmpty(checkMap.get(node.getId()))) {
            node.setChecked(true);
        }

        return getTreeNode(node, areaMap, checkMap, allOnAreaMap);
    }

    public static TreeNode getTreeNode(TreeNode node, Map<String, List<Area>> areaMap, Map<String, String> checkMap, Map<String, Integer> allOnAreaMap) {
        List<Area> areas = areaMap.get(node.getId());
        if (CollectionUtil.isNotEmpty(areas)) {
            for (Area a : areas) {
                if (MapUtils.isEmpty(allOnAreaMap) || allOnAreaMap.get(a.getAreaId()) != null) {
                    TreeNode treeNode = new TreeNode();
                    treeNode.setName(a.getAreaName());
                    treeNode.setId(a.getAreaId());
                    if (checkMap != null && SysUtil.isNotEmpty(checkMap.get(a.getAreaId()))) {
                        treeNode.setChecked(true);
                    }
                    node.getChildren().add(getTreeNode(treeNode, areaMap, checkMap, allOnAreaMap));
                }
            }
        }
        return node;
    }
}
